<?php

namespace App\Models\CacheModel\name_analyze;

use App\Models\Api\BaseCache;

class Zl9NameAnalyzeRecodeBack extends BaseCache
{
    protected $table = 'wechat_backup_07_18_14_43_00.zl9_name_analyze_recode_back';

    protected $cacheModel = 'name_analyze.recode_back';

    public function SaveData(array $data)
    {
        try
        {
            if (is_array($data['recode_info']))
            {
                $data['recode_info'] = json_encode($data['recode_info'], JSON_UNESCAPED_UNICODE);
            }
            $id = $this->insertGetId($data);

            return (int)$id;
        }
        catch (\Exception $e)
        {
            return 0;
        }
    }

    /**
     * @param array $ids
     * @param array|null $type
     * @param string $sortType
     * @return array
     */
    public function getInfoByIdsInData($ids, $type, $sortType)
    {
        $where = ['id' => ['in', $ids]];
        $field = 'id,rid,recode_info';
        try
        {
            $data = $this->field($field)->where($where)->column($field, 'id');
        }
        catch (\Exception $e)
        {
            $data = [];
        }
        return $data;
    }

    public function batchGetInfoByIds($cids, $field = [])
    {
        $resData = parent::batchGetInfoByIds($cids, $field);
        foreach ($resData as $orderId => $orderItemData)
        {
            $resData[$orderId] = arrayKeyData2int($orderItemData, ['id', 'rid']);
            if (isset($orderItemData['recode_info']))
            {
                $resData[$orderId]['recode_info'] = $orderItemData['recode_info'] ? json_decode($orderItemData['recode_info'], true) : [];
            }
        }

        return $resData;
    }
}
